package com.yunc.modules.cms.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yunc.modules.cms.entity.CmsArticleCategory;
import com.yunc.modules.cms.mapper.CmsArticleCategoryMapper;
import com.yunc.modules.cms.service.ICmsArticleCategoryService;
import com.yunc.modules.sys.entity.SysUserRole;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 文章分类关联
 *
 * @author congcong tao
 * @date 2018/12/3 17:39
 */
@Service("cmsArticleCategoryService")
public class CmsArticleCategoryServiceImpl extends ServiceImpl<CmsArticleCategoryMapper, CmsArticleCategory> implements ICmsArticleCategoryService {

    @Autowired
    private CmsArticleCategoryMapper cmsArticleCategoryMapper;

    @Override
    public boolean insertList(Long id, String categoryIds) {
        if (StringUtils.isNotEmpty(categoryIds)) {
            String[] categoryIdArr = categoryIds.split(",");
            if (categoryIdArr != null && categoryIdArr.length > 0) {
                for (String categoryId : categoryIdArr) {
                    CmsArticleCategory articleCategory = new CmsArticleCategory();
                    articleCategory.setArticleId(id);
                    articleCategory.setCategoryId(Long.valueOf(categoryId));
                    boolean flag = this.save(articleCategory);
                    if (!flag) {
                        return flag;
                    }
                }
                return true;
            }
        }
        return false;
    }
}
